package org.community.picture.service.download;

import lombok.Data;
import org.community.picture.repository.dao.CustomFile;
import org.community.picture.repository.util.MinioUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Data
public class DownloadServiceImpl implements DownloadService{

    @Resource
    private MinioUtil util;

    //获取到图片字节流
    private byte[] getPictureBytes(CustomFile customFile){
        MinioUtil downloadUtil = this.getUtil();
        CustomFile file = portraitRename(customFile);
        String fileName = file.getObjectName();
        return downloadUtil.download("portrait/",fileName);
    }


    //生成加密后的字符流
    @Override
    public String getBufferedPortrait(CustomFile customFile) {
        return this.getUtil().bytesToImageBufferedString(this.getPictureBytes(customFile));
    }

    //将消息放入响应体中
    @Override
    public ResponseEntity<byte[]> getResponseBodyPortrait(CustomFile customFile) {
        return this.getUtil().bytesToResponseEntity(this.getPictureBytes(customFile),"");
    }


    @Override
    public String getBufferedPhoto(CustomFile customFile) {
        return null;
    }

    //填充文件名，头像获取请求的文件名为用户id号
    @Override
    public CustomFile portraitRename(CustomFile customFile) {
        String fileName = customFile.getUserId().toString();
        customFile.setObjectName(fileName+".png");
        return customFile;
    }


}
